<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta
			name="viewport"
			content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no"
		/>
		<meta name="format-detection" content="telephone=no" />
		<meta name="format-detection" content="email=no" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Cache-Control" content="no-cache" />
		<meta http-equiv="Expires" content="0" />
		<title>毅瑭科技</title>
		<!-- 引入 vant UI 组件库样式文件 -->
		<link rel="stylesheet" href="../common/css/vant-index.css" />
	</head>

	<body>
		<div id="app" v-cloak></div>
	</body>
	<!-- 上线环境用 -->
	<script src="./base.js"></script>
	<script src="./common/js/vue.global.prod.js?time=202208221056"></script>
	<!-- 引入 UI 组件库 -->
	<script src="./common/js/vant.min.js"></script>
	<!-- axios 接口调用 -->
	<script src="./common/js/axios.min.js"></script>
	<script src="./common/js/url.min.js"></script>
	<!-- <script src="https://cdn.bootcdn.net/ajax/libs/vConsole/3.9.0/vconsole.min.js"></script> -->

	<script type="module">
		// var vConsole = new VConsole()
		import {
			getAction,
			postAction,
			spliceUrl,
			getUrl,
		} from './common/js/api/vue3-axios-utils.js'

		const el = {
			data() {
				return {
					router: {
						role: '/doctor/roleIndex.html',
						doctor: '/doctor/doctorStudio.html',
						patient: '/patient/index.html',
						doctor_menu: '/doctor/menu.html',
						patient_menu: '/patient/menu.html',
					},
				}
			},
			created() {
				this.$toast.loading({
					duration: 0,
					message: '加载中...',
					forbidClick: true,
				})

				const code = url('?code')
				const router = url('?state')
				if (code) {
					// 授权完成，通过code换取用户id，跳转页面
					this.handleGetUserInfoByWechatCode(code, router)
				} else {
					const router = url('?page-router')
					if (!router) {
						this.$toast.clear()
						this.$toast.fail({
							duration: 0,
							message: '异常请求',
						})
					} else {
						const url = encodeURIComponent(this.initCallbackUrl())
						window.location.href = this.createAuthUrl(url, router)
					}
				}
			},
			methods: {
				handleGetUserInfoByWechatCode(code, router) {
					console.log(getUrl())
					postAction(spliceUrl('/sys/wechat/oa/patient/login'), {
						identity: router == 'patient' || router == 'patient_menu' ? 1 : '2',
						jsCode: code,
					}).then(res => {
						res = res.data
						console.log(res)
						if (res.success) {
							let page = this.router[router]
							if (!page) {
								this.$toast.fail({
									duration: 0,
									message: 'redirect error',
								})
							}
							if (
								!res.result.hasPhoneNumber &&
								router != 'patient_menu' &&
								router != 'doctor_menu'
							) {
								if (res.result.identity == 1) {
									window.location.href =
										'/doctor/doctorIndex.html?role=patient&userid=' +
										res.result.userId
								} else if (res.result.identity == 2) {
									window.location.href =
										'/doctor/doctorIndex.html?role=doctor&doctorId=' +
										res.result.userId
								}
							} else {
								if (res.result.identity == 1) {
									window.location.href = page + '?userid=' + res.result.userId
								} else if (res.result.identity == 2) {
									localStorage.setItem('did', res.result.userId)
									window.location.href = page + '?doctorId=' + res.result.userId
								} else {
									this.$toast.clear()
									this.$toast.fail({
										duration: 0,
										message: 'sorry，迷路了',
									})
								}
							}
						} else {
							let message = '数据处理失败'
							if (res.code == 4001) {
								message = '请先关注公众号'
							}

							this.$toast.clear()
							this.$toast.fail({
								duration: 0,
								message,
							})
						}
					})
				},
				initCallbackUrl() {
					const protocol = document.location.protocol + '//'
					let host = window.location.host
					const pathname = window.location.pathname
					if (host.indexOf('test') != -1) {
						host = 'h5.test.dtx-tech.com'
					}

					return protocol + host + pathname
				},
				createAuthUrl(url, router) {
					return (
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx856bd338d5ffb469&redirect_uri=' +
						url +
						'&response_type=code&scope=snsapi_userinfo&state=' +
						router +
						'#wechat_redirect'
					)
				},
			},
		}

		const app = Vue.createApp(el)

		app.use(vant)

		// 通过 CDN 引入时不会自动注册 Lazyload 组件
		// 可以通过下面的方式手动注册
		app.use(vant.Lazyload)

		// 调用函数组件，弹出一个 Toast
		// vant.Toast("刷新成功");

		app.mount('#app')
	</script>
</html>
